package com.jiuyi.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuyi.common.R;
import com.jiuyi.entity.AddressBook;
import com.jiuyi.entity.Category;
import com.jiuyi.exception.SystemException;
import com.jiuyi.mapper.AddressBookMapper;
import com.jiuyi.mapper.CategoryMapper;
import com.jiuyi.service.AddressBookService;
import com.jiuyi.service.CategoryService;
import com.jiuyi.utils.BeanCopyUtils;
import com.jiuyi.vo.AddressBookSelectListVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.jiuyi.emum.HttpMsgMenu.*;

/**
 * DATA: 2022/10/23
 * Author: 晨夕
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;
    //添加收货地址
    @Override
    public R getAddressBook(AddressBook addressBook, HttpServletRequest request) {
        //不能为空
        notNull(addressBook);
        //4. 获取当前用户id
        Object id = request.getSession().getAttribute("user");
        addressBook.setUserId(Long.parseLong(id.toString()));
        addressBookMapper.insert(addressBook);
        return R.success("添加成功");
    }
    //查询收货地址列表
    @Override
    public R<AddressBookSelectListVo> selectAddressBookList(HttpServletRequest request) {
        //获取当前用户id
        Object id = request.getSession().getAttribute("user");
        List<AddressBook> addressBooks = addressBookMapper.selectAddressBookList(Long.parseLong(id.toString()));
        List<AddressBookSelectListVo> addressBookSelectListVos = BeanCopyUtils.copyBeanList(addressBooks, AddressBookSelectListVo.class);
        return R.ok(addressBookSelectListVos);
    }
    //设置默认地址
    @Override
    public R updateDetail(Long id,HttpServletRequest request) {
        //获取当前用户id
        Object userId = request.getSession().getAttribute("user");
        //1. 先将同userId所有的默认地址都变为0
        addressBookMapper.updateDetailToZero(Long.parseLong(userId.toString()));
        //2. 将id下的地址设为默认
        addressBookMapper.updateDetail(id);
        return R.success("默认收货地址设置成功");
    }
    //跟据id查询地址
    @Override
    public R<AddressBookSelectListVo> selectById(Long id) {
        AddressBook addressBook = addressBookMapper.selectById(id);
        AddressBookSelectListVo addressBookSelectListVo = BeanCopyUtils.copyBean(addressBook, AddressBookSelectListVo.class);
        return R.ok(addressBookSelectListVo);
    }
    //修改收货地址
    @Override
    public R updateAddressBook(AddressBook addressBook) {
        //不能为空
        notNull(addressBook);
        addressBookMapper.updateById(addressBook);
        return R.success("修改成功");
    }
    //根据id删除地址
    @Override
    public R deleteAddressBook(Long ids) {
        addressBookMapper.deleteById(ids);
        return R.success("删除成功");
    }
    //获取默认地址
    @Override
    public R<AddressBook> selectDefaultAddressBook(HttpServletRequest request) {
        Object userId = request.getSession().getAttribute("user");
        AddressBook addressBook = addressBookMapper.selectDefaultAddressBook(Long.parseLong(userId.toString()));
        return R.ok(addressBook);
    }

    //不能为空的数据
    public void notNull(AddressBook addressBook){
        //1. 联系人不能为空
        if(StringUtils.isEmpty(addressBook.getConsignee())){
            throw new SystemException(CONSIGNEE_IS_NULL);
        }
        //2. 手机号不能为空
        if(StringUtils.isEmpty(addressBook.getPhone())){
            throw new SystemException(NO_PHONE);
        }
        //3. 地址不能为空
        if(StringUtils.isEmpty(addressBook.getDetail())){
            throw new SystemException(DETAIL_IS_NULL);
        }
    }
}
